/*
	       File: FTPUpload.cs
	Create Date: 2007-12-1
	    Version:

	Description:


*/
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using GuerillaProgrammer.Components;
using GuerillaProgrammer.Components.Logging;
using GuerillaProgrammer.FileProcessor.FTP;

namespace GuerillaProgrammer.FileProcessor.Activities
{
	public partial class FTPUpload: Activity
    {
        #region Private member fields
        private string _SourceFilePath;
        private string _ServerName;
        private string _UploadName;
        private string _UploadDirectory;
        private string _UserID;
        private string _Password;
        #endregion

        #region Constructors and cleanup methods
        public FTPUpload()
        {
            InitializeComponent();
        }
        #endregion

        #region Private implementation methods
        #endregion

        #region Class properties
        public static DependencyProperty SourceFilePathProperty = System.Workflow.ComponentModel.DependencyProperty.Register("SourceFilePath", typeof(string), typeof(FTPUpload));
        [Description("Path to the file being uploaded")]
        [Category("FTP Upload")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string SourceFilePath
        {
            get { return _SourceFilePath; }
            set { _SourceFilePath = value; }
        }
        public static DependencyProperty ServerNameProperty = System.Workflow.ComponentModel.DependencyProperty.Register("ServerName", typeof(string), typeof(FTPUpload));
        [Description("name of the FTP Server")]
        [Category("FTP Upload")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ServerName
        {
            get { return _ServerName; }
            set { _ServerName = value; }
        }

        public static DependencyProperty UploadNameProperty = System.Workflow.ComponentModel.DependencyProperty.Register("UploadName", typeof(string), typeof(FTPUpload));
        [Description("Upload name")]
        [Category("FTP Upload")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string UploadName
        {
            get { return _UploadName; }
            set { _UploadName = value; }
        }

        public static DependencyProperty UploadDirectoryProperty = System.Workflow.ComponentModel.DependencyProperty.Register("UploadDirectory", typeof(string), typeof(FTPUpload));
        [Description("Upload directory")]
        [Category("FTP Upload")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string UploadDirectory
        {
            get { return _UploadDirectory; }
            set { _UploadDirectory = value; }
        }

        public static DependencyProperty UserIDProperty = System.Workflow.ComponentModel.DependencyProperty.Register("UserID", typeof(string), typeof(FTPUpload));
        [Description("User ID for upload")]
        [Category("FTP Upload")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string UserID
        {
            get { return _UserID; }
            set { _UserID = value; }
        }
        public static DependencyProperty PasswordProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Password", typeof(string), typeof(FTPUpload));
        [Description("Password")]
        [Category("FTP Upload")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }
        #endregion

        #region Events handlers
        /// <summary>
        /// Upload the specified file to the directory on the server.
        /// </summary>
        /// <param name="executionContext"></param>
        /// <returns></returns>
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Logger.LogEnter();
            try
            {
                IProcessorDataService dataService = executionContext.GetService(typeof(IProcessorDataService)) as IProcessorDataService;
                if (null == dataService)
                {
                    throw new InvalidOperationException("Can't get the service!");
                }

                WorkItem workItem = dataService.GetWorkItem();
                StringProcessor stringProcessor = new StringProcessor(new FileProcessorSymbolTable(dataService.GetConfiguration(), workItem));

                string sourceFile = stringProcessor.SubstituteStringValues(_SourceFilePath);
                string serverName = stringProcessor.SubstituteStringValues(_ServerName);
                string username = stringProcessor.SubstituteStringValues(_UserID);
                string password = stringProcessor.SubstituteStringValues(_Password);
                string uploadName = stringProcessor.SubstituteStringValues(_UploadName);
                string uploadDirectory = stringProcessor.SubstituteStringValues(_UploadDirectory);

                Logger.LogInfo("FTP", "Logging in to server.\nServer: {0}\nUser: {1}\nPassword: yeah right ;)",serverName, username);
                FtpClient ftpClient = new FtpClient(serverName, username, password);

                ftpClient.Login();
                ftpClient.BinaryMode = true;
                Logger.LogInfo("FTP", "Uploading file to server.\nSource File:{0}\nDestination: {1}", sourceFile, uploadDirectory);
                ftpClient.ChangeDir(uploadDirectory);
                ftpClient.Upload(sourceFile, uploadName, false);
                ftpClient.Close();
                Logger.LogInfo("FTP", "Upload complete");
                Logger.LogLeave();
                return base.Execute(executionContext);
            }
            catch(FtpException ftpExc)
            {
                Logger.LogError("FTPclient error", "An error occured during an FTP operation.\nError: {0}", ftpExc.Message);
                Logger.LogLeave();
                return ActivityExecutionStatus.Faulting;
            }
            catch (Exception exc)
            {
                Logger.LogError("FTP Upload error", "An error occured during upload.\nError: {0}", exc.Message);
                Logger.LogLeave();
                return ActivityExecutionStatus.Faulting;
            }
            
        }
        #endregion
	}
}
